/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            productSpecManageInfo: {
                productSpecs: [],
                total: 0,
                records: 1,
                moreCondition: false,
                specId: '',
                conditions: {
                    specName: '',
                    specId: '',
                    shopId: vc.getCurrentCommunity().shopId
                }
            }
        },
        _initMethod: function () {
            $that._listProductSpecs(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function () {
            vc.on('productSpecManage', 'listProductSpec', function (_param) {
                $that._listProductSpecs(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            vc.on('pagination', 'page_event', function (_currentPage) {
                $that._listProductSpecs(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _listProductSpecs: function (_page, _rows) {
                $that.productSpecManageInfo.conditions.page = _page;
                $that.productSpecManageInfo.conditions.row = _rows;
                var param = {
                    params: $that.productSpecManageInfo.conditions
                };
                param.params.specName = param.params.specName.trim();
                param.params.specId = param.params.specId.trim();
                //发送get请求
                vc.http.apiGet('/product/queryProductSpec',
                    param,
                    function (json, res) {
                        var _productSpecManageInfo = JSON.parse(json);
                        $that.productSpecManageInfo.total = _productSpecManageInfo.total;
                        $that.productSpecManageInfo.records = _productSpecManageInfo.records;
                        let _data = _productSpecManageInfo.data;
                        _data.forEach(item => {
                            if (!item.hasOwnProperty('productSpecDetails')) {
                                return;
                            }
                            let _productSpecDetails = item.productSpecDetails;
                            let _specValue = "";
                            _productSpecDetails.forEach(detailItem => {
                                _specValue += (detailItem.detailName + ':' + detailItem.detailValue + "<br/>");
                            });
                            item.specValue = _specValue;
                        });
                        $that.productSpecManageInfo.productSpecs = _data;
                        vc.emit('pagination', 'init', {
                            total: $that.productSpecManageInfo.records,
                            dataCount: $that.productSpecManageInfo.total,
                            currentPage: _page
                        });
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _openAddProductSpecModal: function () {
                vc.emit('addProductSpec', 'openAddProductSpecModal', {});
            },
            _openEditProductSpecModel: function (_productSpec) {
                vc.emit('editProductSpec', 'openEditProductSpecModal', _productSpec);
            },
            _openDeleteProductSpecModel: function (_productSpec) {
                vc.emit('deleteProductSpec', 'openDeleteProductSpecModal', _productSpec);
            },
            //查询
            _queryProductSpecMethod: function () {
                $that._listProductSpecs(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            //重置
            _resetProductSpecMethod: function () {
                $that.productSpecManageInfo.conditions.specName = "";
                $that.productSpecManageInfo.conditions.specId = "";
                $that._listProductSpecs(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _moreCondition: function () {
                if ($that.productSpecManageInfo.moreCondition) {
                    $that.productSpecManageInfo.moreCondition = false;
                } else {
                    $that.productSpecManageInfo.moreCondition = true;
                }
            }
        }
    });
})(window.vc);
